package com.hsnn.medstgmini.pro.plan.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.plan.dao.BasePriceadjplandetailDao;
import com.hsnn.medstgmini.util.Pagination;

@Repository
public class BasePriceadjplandetailDaoImpl extends BaseHibernateDAO implements
		BasePriceadjplandetailDao {
	@Autowired
	private HibernateSqlUtil dao;
	
	/**
	 * 
	 * @category 获取调价计划明细列表
	 * @auth wangbing
	 * @return Pagination
	 * @date 2015年6月23日
	 */
	@Override
	public Pagination getPriceDurgList(Pagination page) {
		String drugComp = page.getConditions().get("compName");
		String projName = page.getConditions().get("projName");
		String goodsCode = page.getConditions().get("goodsCode");
		String drugName = page.getConditions().get("drugName");
		String drugCode = page.getConditions().get("drugCode");
		String priceid = page.getConditions().get("priceid");
		StringBuffer sql = new StringBuffer()
		.append("	SELECT a.price_adj_paln_detail_id priceAdjPalnDetailId,a.price_adj_plan_id priceAdjPlanId,")
		.append("	b.proj_name name,b.drug_code drugCode,b.qualityspec_name drugName,b.form_name formName,")
		.append("	b.factor factor,b.unit_name unit,b.prod_compname compName,b.material_name materialName,b.prod_compcode prodCompId,")
		.append("	a.orig_pro_price_limit origProPriceLimit,a.curr_pro_price_limit currProPriceLimit,b.drug_code drugId,")
		.append("	a.orig_retail_price_limit origRetailPriceLimit,a.curr_retail_price_llimit currRetailPriceLlimit,")
		.append("	a.add_datetime addDatetime,a.goods_code goodsCode,b.spec_name specName")
		.append("	FROM base_priceadjplandetail a LEFT JOIN base_goods_item  b ON b.goods_code = a.goods_code ")
		.append("	WHERE a.price_adj_plan_id = :priceid ");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("priceid", priceid);
		if(StringUtils.isNotBlank(drugComp)){
			sql.append(" and (b.prod_compname like :drugComp or b.prod_compname_spel like :drugComp)");
			params.put("drugComp", "%"+drugComp+"%");
		}
		if(StringUtils.isNotBlank(projName)){
			sql.append(" and b.proj_name like :projName ");
			params.put("projName", "%"+projName+"%");
		}
		if(StringUtils.isNotBlank(goodsCode)){
			sql.append(" and a.goods_code = :goodsCode ");
			params.put("goodsCode", goodsCode);
		}
		if(StringUtils.isNotBlank(drugName)){
			sql.append(" and (b.qualityspec_name like :drugName or b.qualityspec_name_spel like :drugName) ");
			params.put("drugName", "%"+drugName+"%");
		}
		if(StringUtils.isNotBlank(drugCode)){
			sql.append(" and b.drug_code = :drugCode ");
			params.put("drugCode", drugCode);
		}

		return dao.getPageMapList(sql.toString(), params, page);
	}
	
	/**
	 * 
	 * @category 获取历史调价信息
	 * @auth wangbing
	 * @return Pagination
	 * @date 2015年6月23日
	 */
	@Override
	public Pagination getHistoryDataList(Pagination page) {
		String goodsCode = page.getConditions().get("goodsCode");
		String drugCode = page.getConditions().get("drugCode");
		String drugName = page.getConditions().get("drugName");
		String prodComp = page.getConditions().get("prodComp");
		String projName = page.getConditions().get("projName");
		String endTime = page.getConditions().get("endTime");
		String startTime = page.getConditions().get("startTime");
		String orgId = page.getConditions().get("orgId");
		StringBuffer sql = new StringBuffer()
		.append("	SELECT a.price_adj_paln_detail_id priceAdjPalnDetailId,a.price_adj_plan_id priceAdjPlanId,t.execute_datetime executeDatetime,")
		.append("	b.proj_name projName,b.drug_code drugCode,b.qualityspec_name drugName,b.form_name formName,")
		.append("	b.factor factor,b.unit_name unit,b.prod_compname prodComp,b.material_name materialName,b.prod_compcode prodCompId,")
		.append("	a.orig_pro_price_limit origProPriceLimit,a.curr_pro_price_limit currProPriceLimit,b.drug_code drugId,")
		.append("	a.orig_retail_price_limit origRetailPriceLimit,a.curr_retail_price_llimit currRetailPriceLlimit,")
		.append("	a.add_datetime addDatetime,a.goods_code goodsCode,b.spec_name specName,t.price_adj_accord priceAdjAccord")
		.append("	FROM base_priceadjplan t left join base_priceadjplandetail a on a.price_adj_plan_id = t.price_adj_plan_id ")
		.append("   LEFT JOIN base_goods_item  b ON b.goods_code = a.goods_code")
		.append("	WHERE t.status = 1 and t.create_org_id = :orgId ");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("orgId", orgId);
		if(StringUtils.isNotBlank(prodComp)){
			sql.append(" and (b.prod_compname like :prodComp or b.prod_compname_spel like :prodComp)");
			params.put("prodComp", "%"+prodComp.trim()+"%");
		}
		if(StringUtils.isNotBlank(projName)){
			sql.append(" and b.proj_name like :projName ");
			params.put("projName", "%"+projName.trim()+"%");
		}
		if(StringUtils.isNotBlank(goodsCode)){
			sql.append(" and a.goods_code = :goodsCode ");
			params.put("goodsCode", goodsCode.trim());
		}
		if(StringUtils.isNotBlank(drugName)){
			sql.append(" and (b.qualityspec_name like :drugName or b.qualityspec_name_spel like :drugName) ");
			params.put("drugName", "%"+drugName.trim()+"%");
		}
		if(StringUtils.isNotBlank(drugCode)){
			sql.append(" and b.drug_code = :drugCode ");
			params.put("drugCode", drugCode.trim());
		}
		if(StringUtils.isNotBlank(startTime)){
			sql.append(" and t.execute_datetime >= :startTime ");
			params.put("startTime", startTime+" 00:00:00");
		}
		if(StringUtils.isNotBlank(endTime)){
			sql.append(" and t.execute_datetime <= :endTime ");
			params.put("endTime", endTime+" 23:59:59");
		}
		sql.append(" order by t.execute_datetime desc ");
		return dao.getPageMapList(sql.toString(), params, page);
	}
	

}